import Dep from "./Dep"
import  Observer  from "./Observer"
import { isObject, observe } from "../common"

export default function defineReactive (target: object, key: string, value: any) {
    if(typeof value === 'object') {
        new Observer(value)
    }
    let childOb = observe(value)
    let dep  = new Dep()
    let val = value
    Object.defineProperty(target, key, {
        enumerable: true,
        configurable: true,
        get: function () {
            dep.depend()
            if(childOb) {
                childOb.dep.depend()
            }
            return val
        },
        set: function (newVal) {
            if(newVal === val) return
            dep.notify()
            val = newVal
        }
    })
}